/*	全局变量*/
		var page=0;//页码
		var loading=true;//标记请求状态防止多次请求
		var config={
			timeout:7000,
			parkLotSn:null
		};
/*  函数
	handlRender--渲染模板函数
	getData---请求数据
*/
/*		初始化函数*/
if ('addEventListener' in document) {
	document.addEventListener('DOMContentLoaded', function() {
		FastClick.attach(document.body);
	}, false);
}
$(function(){
		getData();
		getParklot();
		FastClick.attach(document.body);
	});		
		function handlRender(tpl,tpl_data,appendBox){
		        var tpl=tpl.html(),
		            template = Handlebars.compile(tpl),
		            tplHtml = template(tpl_data);
		            appendBox.append(tplHtml)
		};
//支付格式转换，mdb，lj handebar
		Handlebars.registerHelper('Convert', function(word, options){
			var newWord=word=='weixin'?"微信":"支付宝";
	          return newWord;
	     });		
		Handlebars.registerHelper('Convert_pay', function(word, options){
			  var newWord=word=='notpay'?"未支付":"支付成功";
	          return newWord;
	     });
		//发票状态
		Handlebars.registerHelper('Convert_invoiceStatus', function(word, options){
			var newWord=word=='invoiceWait'?"开票中":(word=='success'?'开票成功':'未开票');
			return newWord;
		});
		Handlebars.registerHelper('Convert_invoiceBt', function(p, r,i){
			if(p=='success'&&r==0&&i!=='invoiceWait'&&i!=='success'){
				newWord='开票'
			}else{
				newWord='禁用'
			}
			return newWord;
		});
		Handlebars.registerHelper('Convert_invoice', function(p, r,i){

			var newWord='';
			// p 支付状态
			// r 退款金额==0
			// i 开票状态
			if(p=='success'&&r==0&&i!=='invoiceWait'&&i!=='success'){
				newWord='able'
			}else{
				newWord='disable'
			}
			return newWord;
		});
		function getData(){
			$('#loadMore').show();
			page++;
			var data=JSON.stringify({pageNumber:page});
			$.ajax({
				data:data,
				dataType:"json",
				contentType: "application/json;charset=utf-8",
			    type:'post',
				success:function(result){
/* 	如果有数据--$('#wrapper .empty').hide(); */
					$('#loadMore').hide();
					if(result.datas){
						var data=result.datas.content;
						var len=data.length;
						if(len>0){
							loading=false;
							$('#wrapper .empty').hide();
							handlRender($('#plateTpl'),result.datas,$('#list')); 
						}else{	
							if(page==1){
/* 第一次加载数组为空--没数据 */
								$('#wrapper .empty').show();
							}else{
/* 	数据加载完了没数据 */
								$('#loadMore').show();	
								$('#loadMore').html('没有更多数据了……');																	
								$('#loadMore').fadeOut(500);								
								
							}		
						}						
					};

				}
			})  						
		};
// 滚动加载
		$(window).scroll(function(){
			var isBottom=$(window).scrollTop() + $(window).height() == $(document).height();
			var isTop=$(window).scrollTop()==0;
			if(isBottom){
				if(!loading){
					loading=true;
					if(config.parkLotSn!==null){
						getInvoicelist();
					}else{
						getData();
					}


				}								
			}		 
		});
// /    get请求     获取车场列表，包含车场名与车场编号
		;function getParklot() {
			httpfx('/member/order/parking/list','','get',function (res) {
				var status=res.status;
				if(status==0){
					var len=res.datas.length;
					var list=res.datas;
					var parkSelectHtml='';
					list.forEach(function(item){
						parkSelectHtml+='<li data-id="'+item.value+'">'+item.name+'</li>'
					});
					$('#select_parkSelect').append(parkSelectHtml);

				}else{
					layer.msg(res.err, {icon: 5,shade: [0.8, '#393D49'],time: 1500});
				}
			},function (res) {
				layer.msg(res.err, {icon: 5,shade: [0.8, '#393D49'],time: 1500});
			})
		}
//获取发票列表
		;function getInvoicelist(){
			$('#loadMore').show();
			page++;
			var askData={};
			if(config.parkLotSn!==null){
				askData.searchValue=config.parkLotSn
			}
			askData.pageNumber=page
			var serchData=JSON.stringify(askData);
			httpfx('/member/order/invoice/order',serchData,'post',function (res) {
					$('#loadMore').hide();
					if(res.datas){
						var data=res.datas.content;
						var len=data.length;
						if(len>0){
							loading=false;
							$('#wrapper .empty').hide();
							handlRender($('#plateTpl'),res.datas,$('#list'));
						}else{
							if(page==1){
								$('#wrapper .empty').show();
							}else{
								/* 	数据加载完了没数据 */
								$('#loadMore').show();
								$('#loadMore').html('没有更多数据了……');
								$('#loadMore').fadeOut(500);

							}
						}
					};
			},function (res) {
				console.log(res)
			})
		}
/*
/member/invoice/qrcode
 post
获取开发票的链接	*/
		;function getInvoiceUrl(params) {
			var selfUrl='/member/invoice/qrcode'+params;
			httpfx(selfUrl,'','post',function (res) {
				if (res.status === 0) {
						var url=res.datas;
							window.open(url)
				}else{
					layer.msg(res.err, {icon: 5,shade: [0.8, '#393D49'],time: 1500});
				}
			},function (res) {
				layer.msg(res.err, {icon: 5,shade: [0.8, '#393D49'],time: 1500});
			})
		}
// 请求封装
		;function httpfx(url,serchData,method,sufx,errfx){
			$.ajax({
				url:url,
				type:method,
				dataType:"json",
				data:serchData,
				timeout:config.timeout,
				contentType: "application/json;charset=utf-8",
				success:function (res) {
					sufx(res)
				},
				complete: function(XMLHttpRequest, textStatus) {
					try{
						if(textStatus=='timeout'){
							var xmlhttp = window.XMLHttpRequest ? new window.XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHttp");
							xmlhttp.abort();
							console.log('网络超时！')
						}
					}catch (res) {
						errfx(res)
					}

				},
				error: function(res){
					errfx(res)
				}
			})
		};
// 点击开发票-单独开票
	$('#wrapper').on('click','.invoiceBt',function(){
		var disable=$(this).hasClass('disable');
			if(!disable){
				var parkingLotSn=$(this).data('sn'),
					ids=$(this).data('ids');
				var params='?ids='+ids+'&parkingLotSn='+parkingLotSn;
					getInvoiceUrl(params);
			}else{
				var selfMsg='无效的操作！'
				layer.msg(selfMsg, {icon: 2,shade: [0.8, '#393D49'],time: 1500});
			}
	});
	$('#wrapper').on('click','.mergeBt',function(){
		$(this).toggleClass('mergeBtchecked');
		var len=$('.mergeBtchecked').length;
		$('.listNumber').text(len);
	});
// 车场列表，点击下拉按钮
	$('.parkSelect').click(function () {
		var isShow=$('.sizerWrap').hasClass('active');
		if(isShow){
			$('.sizerWrap').removeClass('active');
			$(this).removeClass('active');
		}else{
			$('.sizerWrap').addClass('active');
			$(this).addClass('active');

		}
	});
// 点击车场按钮
	$('#select_parkSelect').on('click','li',function(){
		$('#list').html('');
		config.parkLotSn=$(this).data('id');
		page=0;//页码
		$('.sizerWrap').removeClass('active');
		$('.parkSelect').removeClass('active');
		$(this).addClass('checked').siblings('li').removeClass('checked');
		if(config.parkLotSn!==null){
			$('#list').removeClass('parklotCheck');
			$('.addInvoice-wrap').show();
			getInvoicelist();
		}else{
			$('#list').addClass('parklotCheck');
			$('.addInvoice-wrap').hide();
			getData();

		}
	});
//确认合并-合并开票
	$('.invoiceSubmit').click(function () {
		var $items=$('.mergeBtchecked');
		var idsList=''
		var idsArr=[];
		$items.each(function (index,item) {
			var self=$(this);
			var $ids=self.data('ids');
			idsArr.push($ids)
		});
		idsArr.forEach(function (item,index) {
			if(index>0){
				idsList+='&ids='+item
			}else if(index==0) {
				idsList += 'ids=' + item
			}
		});
		var parkingLotSn=$(this).data('sn');
		var parkingLotSn=$('#select_parkSelect').find('.checked').data('id');
		var params='?'+idsList+'&parkingLotSn='+parkingLotSn;
			getInvoiceUrl(params)
	});